<?php
if (!isset($_POST['send'])) {
    if (isset($_SESSION['username'])) {
        $nev = $_SESSION['username'];
    } else {
        $nev = "";
    }
    require_once 'dbconnect.php';
    $sql = mysql_query("SELECT email FROM users WHERE username='$nev'") or die(mysql_error());
    $rekord = mysql_fetch_array($sql);
    ?>
    <div class="kapcsolat">
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>?show=ask" method="post" onsubmit="return check()">
            Név<span>*</span><br/>
            <input type="text" name="sender_name" id="sender_name" value="<?php
            if (isset($_SESSION['sender_name'])) {
                echo $_SESSION['sender_name'];
            } else {
                echo $nev;
            }
            ?>"><br/>
            Cím<span>*</span><br/>
            <input type="text" name="sent_from" id="sent_from" value="<?php
            if (isset($_SESSION['sent_from'])) {
                echo $_SESSION['sent_from'];
            } else {
                echo $rekord['email'];
            }
            ?>"><br/>
            Tárgy<span>*</span><br/>
            <input type="text" name="subject" id="subject" value="<?php
            if (isset($_SESSION['subject'])) {
                echo $_SESSION['subject'];
            } else {
                echo '';
            }
            ?>"><br/>
            Üzenet<span>*</span><br/>
            <textarea name="message" id="message"><?php
                if (isset($_SESSION['message'])) {
                    echo $_SESSION['message'];
                } else {
                    echo '';
                }
                ?></textarea><br/>
            <div id="user_data">
                <img src="captcha.php" title="biztonsági kód"><br/>
                <input type="text" name="kod" id="kod" maxlength="6"><br/>
                <input type="submit" name="send" value="Elküld"><br/>
            </div>
            <span>*</span> Kötelező kitölteni
        </form>
    </div>
    <?php
} elseif (strtolower($_POST['kod']) !== $_SESSION['captcha']) {
    echo 'Rossz biztonsági kód!';
    $_SESSION['sender_name'] = $_POST['sender_name'];
    $_SESSION['sent_from'] = $_POST['sent_from'];
    $_SESSION['subject'] = $_POST['subject'];
    $_SESSION['message'] = $_POST['message'];
    header("refresh: 3;" . $_SERVER['PHP_SELF'] . "?show=ask");
} elseif (!empty($_POST['sender_name']) && !empty($_POST['sent_from']) && !empty($_POST['subject']) && !empty($_POST['message'])) {
    require_once 'phpmailer/class.phpmailer.php';
    $email = $_POST['sent_from'];
    $name = $_POST['sender_name'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->Username = 'lokos.bela@gmail.com';
    $mail->Password = 'theetium';
    $mail->SetFrom($email, $name);
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->IsHTML(true);
    $address = "lokos.bela@gmail.com";
    $mail->AddAddress($address);
    if (!$mail->Send()) {
        echo '<div class="mail">' . $mail->ErrorInfo . '</div>';
    } else {
        echo '<div class="mail">Üzenetét megkaptuk, hamarosan felvesszük Önnel a kapcsolatot.</div>';
    }
}
?>